home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / QT MovieToolBox / Dialogs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-19  |  10.4 KB  |  435 lines  |  [TEXT/KAHL]

  1. #include "gGlobals.h"
  2.  
  3. #include    <pascal.h>
  4. #include    <Scrap.h>
  5. #include    <Desk.h>
  6. #include    <string.h>
  7. #include    <stdio.h>
  8. #include    <Fonts.h>
  9. #include    <Math.h>
  10.  
  11. /*********************************************
  12.  
  13.     Allow the user to set the compression parameters with the standard compression dialog.
  14.     
  15. *********************************************/
  16.  
  17. void InitSetCompression()
  18. {
  19.     gStandardP.flags = scShowMotionSettings;
  20.     gStandardP.theCodecType = kCodecType;
  21.     gStandardP.theCodec = kCodecID;
  22.     gStandardP.spatialQuality = kCodecQuality;
  23.     gStandardP.temporalQuality = 0;
  24.     gStandardP.depth = kCodecDepth;
  25.     gStandardP.frameRate = kFrameRate<<16;
  26.     gStandardP.keyFrameRate = kFrameRate;
  27. }    /* End of () */
  28.  
  29. /*********************************************
  30.  
  31.     Allow the user to set the compression parameters with the standard compression dialog.
  32.     
  33. *********************************************/
  34.  
  35. SetCompression()
  36. {    ComponentInstance ci;
  37.     Point where;
  38.     
  39.     where.h = where.v = -2;
  40.     ci = OpenDefaultComponent(StandardCompressionType,0);
  41.     if ( ci == nil )  {
  42.         Error("Couldn't open StdCompression",0);    return;
  43.     }
  44.  
  45.     SCGetCompression(ci,&gStandardP,where);
  46.     
  47. }    /* End of () */
  48.  
  49. /*
  50.  
  51.     Allow the user to set the number of stages she wants the processing to take.
  52. */
  53. SetStages()
  54. {
  55. #define    STAGE_DLOG    128
  56. #define    IT_STAGES    3
  57. #define    IT_LINEAR    4
  58. #define    IT_OK    1
  59. #define    IT_CANCEL    2
  60.  
  61.     CGrafPtr     savePort;
  62.     DialogPtr    theDialog;
  63.     short        itemType;
  64.     Handle        itemHdl;
  65.     Rect        itemRect;
  66.     short        itemHit,lastItem = -1;
  67.     Boolean        done = false;
  68.     Point         tp;
  69.     GDHandle    saveGD;
  70.     Str255        text;
  71.     long        num;
  72.     long        stageCount = gNumberSteps;
  73.     Boolean        linear = true;
  74.     
  75.     if  ((theDialog = GetNewDialog(STAGE_DLOG, nil, (WindowPtr) -1)) == nil )
  76.         return;
  77.  
  78.     GetMouse(&tp);
  79.     GetGWorld(&savePort,&saveGD);
  80.     SetPort(theDialog);
  81.     GetDItem(theDialog,IT_OK,&itemType,&itemHdl,&itemRect);
  82.     PenSize(3,3);
  83.     InsetRect(&itemRect,-4,-4);
  84.     FrameRoundRect(&itemRect,16,16);
  85.  
  86.     
  87.     GetDItem(theDialog,IT_STAGES,&itemType,&itemHdl,&itemRect);
  88.     NumToString(stageCount, text);
  89.     SetIText(itemHdl,text);
  90.  
  91.     SelIText(theDialog,IT_STAGES,0,32767);
  92.  
  93.     GetDItem(theDialog,IT_LINEAR,&itemType,&itemHdl,&itemRect);
  94.     SetCtlValue((ControlHandle)itemHdl,linear);
  95.     HiliteControl((ControlHandle)itemHdl,255);
  96.     
  97.     while ( !done ) {
  98.         ModalDialog(nil, &itemHit);
  99.         if ( itemHit != lastItem ) {
  100.             switch( lastItem ) { 
  101.             case IT_STAGES :
  102.                 GetDItem(theDialog,IT_STAGES,&itemType,&itemHdl,&itemRect);
  103.                 GetIText(itemHdl,text);
  104.                 StringToNum(text, &num);
  105.                 if ( num > 0  ) stageCount = num;
  106.                 else {    SysBeep(1);
  107.                     NumToString(stageCount, text);
  108.                     SetIText(itemHdl,text);
  109.                     if (itemHit == IT_OK) itemHit = -1;
  110.                     }
  111.                 break;
  112.             }
  113.             lastItem = itemHit;
  114.         }
  115.         
  116.         switch  ( itemHit  ) {
  117.         case IT_OK :
  118.             gNumberSteps = stageCount;
  119.             done = true;
  120.             break;
  121.         case IT_CANCEL :    done = true;
  122.             break;
  123.         case IT_LINEAR:
  124.             linear = !linear;
  125.             GetDItem(theDialog,IT_LINEAR,&itemType,&itemHdl,&itemRect);
  126.             SetCtlValue((ControlHandle)itemHdl,linear);
  127.             break;
  128.         }
  129.     }
  130.     CloseDialog(theDialog);
  131.     SetGWorld(savePort,saveGD);
  132. }    /* End of () */
  133.  
  134.  
  135. /********************************************
  136.  
  137.     Allocate the image buffers for the processing
  138.     
  139. ********************************************/
  140.  
  141. OSErr AllocateBuffers()
  142. {
  143.  
  144.     Rect    rect = gRect;
  145.     OSErr result = 0;
  146.     short depth = gDepth  > 32 ? (gDepth-32) : gDepth;
  147.     CTabHandle    clut = nil;
  148.     
  149.     OffsetRect(&rect,-rect.left,-rect.top);
  150.     
  151.     if ( gDepth > 32 ) 
  152.         clut = GetCTable(gDepth);
  153.         
  154.     if ( gDstWorld ) {
  155.         DisposeGWorld(gDstWorld);
  156.         gDstWorld = nil;
  157.         }
  158.     if ( gBufferWorld ) {
  159.         DisposeGWorld(gBufferWorld);
  160.         gBufferWorld = nil;
  161.         }
  162.     if ( (result=NewGWorld(&gWorld,depth,&rect,clut,nil,0)) != 0 ) {
  163.         if ( (result=NewGWorld(&gWorld,depth,&rect,clut,nil,useTempMem)) != 0 ) {
  164.             Error("NewGWorld Failed",result);    goto done;
  165.         }
  166.     }
  167.     if ( (result=NewGWorld(&gAltWorld,depth,&rect,clut,nil,0)) != 0 ) {
  168.         if ( (result=NewGWorld(&gAltWorld,depth,&rect,clut,nil,useTempMem)) != 0 ) {
  169.             Error("NewGWorld Failed",result);    goto done;
  170.         }
  171.     }
  172.     if ( (result=NewGWorld(&gBufferWorld,depth,&rect,clut,nil,0)) != 0 ) {
  173.         if ( (result=NewGWorld(&gBufferWorld,depth,&rect,clut,nil,useTempMem)) != 0 ) {
  174.             Error("NewGWorld Failed",result);    goto done;
  175.         }
  176.     }
  177.     if ( (result=NewGWorld(&gDstWorld,depth,&rect,clut,nil,0)) != 0 ) {
  178.         if ( (result=NewGWorld(&gDstWorld,depth,&rect,clut,nil,useTempMem)) != 0 ) {
  179.             Error("NewGWorld Failed",result);    goto done;
  180.         }
  181.     }
  182. done:
  183.     if ( result ) {
  184.         if ( gWorld )        DisposeGWorld(gWorld);
  185.         gWorld= nil;
  186.         if ( gAltWorld )    DisposeGWorld(gAltWorld);
  187.         gAltWorld= nil;
  188.         if ( gDstWorld )    DisposeGWorld(gDstWorld);
  189.         gDstWorld= nil;
  190.         if ( gBufferWorld )    DisposeGWorld(gBufferWorld);
  191.         gBufferWorld= nil;
  192.     }
  193.     if ( clut )                DisposeCTable(clut);
  194.     return(result);        
  195. }    /* End of () */
  196.  
  197.  
  198. /*********************************************
  199.  
  200.     Show a preview of some stage in the process.
  201.     
  202. ********************************************/
  203. DoPreview()
  204. {
  205. #define    PV_DLOG    129
  206. #define    IT_STAGE    4
  207. #define    IT_OK    1
  208. #define    IT_CANCEL    2
  209.  
  210.     CGrafPtr     savePort;
  211.     DialogPtr    theDialog;
  212.     short        itemType;
  213.     Handle        itemHdl;
  214.     Rect        itemRect;
  215.     short        itemHit,lastItem = -1;
  216.     Boolean        done = false;
  217.     Point         tp;
  218.     GDHandle    saveGD;
  219.     Str255        text;
  220.     long        num;
  221.     long        whichStage = gNumberSteps/2;
  222.     
  223.  
  224.     if  ((theDialog = GetNewDialog(PV_DLOG, nil, (WindowPtr) -1)) == nil )
  225.         return;
  226.  
  227.     GetMouse(&tp);
  228.     GetGWorld(&savePort,&saveGD);
  229.     SetPort(theDialog);
  230.     GetDItem(theDialog,IT_OK,&itemType,&itemHdl,&itemRect);
  231.     PenSize(3,3);
  232.     InsetRect(&itemRect,-4,-4);
  233.     FrameRoundRect(&itemRect,16,16);
  234.  
  235.     
  236.     GetDItem(theDialog,IT_STAGE,&itemType,&itemHdl,&itemRect);
  237.     NumToString(whichStage, text);
  238.     SetIText(itemHdl,text);
  239.  
  240.     SelIText(theDialog,IT_STAGE,0,32767);
  241.  
  242.     while ( !done ) {
  243.         ModalDialog(nil, &itemHit);
  244.         
  245.         if ( itemHit != lastItem ) {        /* Check number, if user goes to another control */
  246.             switch( lastItem ) { 
  247.             case IT_STAGE :
  248.                 GetDItem(theDialog,IT_STAGE,&itemType,&itemHdl,&itemRect);
  249.                 GetIText(itemHdl,text);
  250.                 StringToNum(text, &num);
  251.                 if ( num > 0  && num <= gNumberSteps ) 
  252.                     whichStage = num;
  253.                 else    {
  254.                     SysBeep(1);
  255.                     NumToString(whichStage,text);
  256.                     SetIText(itemHdl,text);
  257.                     if (itemHit == IT_OK) itemHit = -1;
  258.                     }
  259.                 break;
  260.             }
  261.             lastItem = itemHit;
  262.         }
  263.         
  264.         switch  ( itemHit  ) {
  265.         case IT_OK :    done = true;
  266.             break;
  267.         case IT_CANCEL :    whichStage = 0;
  268.             done = true;
  269.             break;
  270.         }
  271.     }
  272.     CloseDialog(theDialog);
  273.     
  274.     if ( whichStage ) {
  275.         Str15 numStr;
  276.         Rect    rect;
  277.         
  278.         BlockMove("\pPreview ",text,10);
  279.         NumToString(whichStage, numStr);
  280.         BlockMove(&numStr[1],&text[text[0]+1],(Size)numStr[0]);
  281.         text[0] = text[0] + numStr[0];
  282.         
  283.         rect = gSrcWindow->portRect;
  284.         SetGWorld(gSrcWindow,nil);
  285.         LocalToGlobal((Point *)&rect.top);
  286.         LocalToGlobal((Point *)&rect.bottom);
  287.         OffsetRect(&rect,20,20);
  288.         if ( gOversample ) {
  289.             rect.right = rect.left + (rect.right-rect.left)/2;
  290.             rect.bottom = rect.top + (rect.bottom-rect.top)/2;
  291.         }
  292.         SetGWorld(savePort,saveGD);
  293.         if ( gDstWindow ) {
  294.             CloseWindow((WindowPtr)gDstWindow);
  295.             gDstWindow = nil;
  296.         }
  297.         if ( (gDstWindow = (CWindowPtr)NewCWindow(nil,&rect,text,true,
  298.                     zoomDocProc,(WindowPtr)-1,true,0)) == nil ) {
  299.             Error("NewCWindow Failed",0);
  300.             goto done;
  301.         }
  302.         BringToFront((WindowPtr)gDstWindow);
  303.         HiliteWindow((WindowPtr)gDstWindow,true);
  304.         SetGWorld((CGrafPtr)gDstWorld,nil);
  305.         PaintRect(&gDstWorld->portRect);
  306.  
  307.  
  308.         if ( gDstWorld && gWorld && gAltWorld ) 
  309.             DoStage(whichStage,gNumberSteps,gBackwards);
  310.  
  311.         SetGWorld((CGrafPtr)gDstWindow,nil);
  312.         CopyBits((BitMap *)*gDstWorld->portPixMap,(BitMap *)*gDstWindow->portPixMap,
  313.                 &gDstWorld->portRect,&gDstWindow->portRect,ditherCopy,nil);
  314.     }
  315. done:
  316.     SetGWorld(savePort,saveGD);
  317. }    /* End of () */
  318.  
  319. /********************************************
  320.  
  321.     Make a source window 
  322.     
  323. ********************************************/
  324.  
  325. CWindowPtr    MakeSWindow(Rect *frame,StringPtr name,GWorldPtr buffer)
  326.  
  327. {    CWindowPtr    wind;
  328.  
  329.     if ( (wind = (CWindowPtr)NewCWindow(nil,frame,name,false, zoomDocProc,(WindowPtr)-1,true,0)) == nil ) {
  330.         Error("NewCWindow Failed",0);
  331.         return(nil);
  332.     }
  333.     ShowWindow((WindowPtr)wind);
  334.     BringToFront((WindowPtr)wind);    HiliteWindow((WindowPtr)wind,true);
  335.     SetWRefCon((WindowPtr)wind,(long)buffer);
  336.     return(wind);
  337. }    /* End of () */
  338.  
  339. /********************************************
  340.  
  341.     Open a file.
  342.     
  343. ********************************************/
  344.  
  345. OSErr    DoOpen(FSSpec *fsp)
  346. {    long        result = noErr;
  347.     Rect         zpFrame;
  348.     GWorldPtr    saveWorld;
  349.     GDHandle    saveGD;
  350.     short        oFile = -1;
  351.     StandardFileReply    theSFR;    
  352.     Boolean HasCompression = true;
  353.     long resp;
  354.     
  355.     if ( Gestalt(gestaltCompressionMgr, &resp) != noErr || resp < 15 )
  356.         HasCompression = false;
  357.  
  358.     
  359.     GetGWorld(&saveWorld,&saveGD);
  360.     if ( fsp == nil ) {
  361.         SFTypeList     types = { 'PICT','JFIF',0 };
  362.         if ( gHasNewStdFile ) {
  363.             if (!HasCompression)    StandardGetFile(nil,2,types,&theSFR);
  364.             else    StandardGetFilePreview(nil,2,types,&theSFR);
  365.         } else {
  366.             SFReply        osfr;
  367.             Point    pt = {100,100};
  368.             
  369.             SFGetFile(pt,(ConstStr255Param)"",nil,1,types,nil,&osfr);
  370.             theSFR.sfGood = osfr.good;
  371.             theSFR.sfReplacing = osfr.copy;
  372.             theSFR.sfType = osfr.fType;
  373.             if ( osfr.good ) 
  374.                 FSMakeFSSpec(osfr.vRefNum,0L,osfr.fName,&theSFR.sfFile);
  375.         }
  376.         if ( !theSFR.sfGood  ) {
  377.             return(1);
  378.         }
  379.     } else {
  380.         theSFR.sfFile = *fsp;
  381.     }
  382.         
  383.     if ( FSpOpenDF(&theSFR.sfFile,fsRdPerm,&oFile) ) {
  384.         result = -1;
  385.         goto done;
  386.     }
  387.  
  388.     
  389.     /************************************************
  390.      *
  391.      *    Get the picture frame, to see how big of a window to make.
  392.      *
  393.      ************************************************/
  394.  
  395.     if ( GetPictureFileHeader(oFile,&gOriginalPicFrame,nil ) ) {
  396.         result = -1;
  397.         goto done;
  398.     }
  399.  
  400.     if ( gSrcWindow == nil ) {
  401.     
  402.         OffsetRect(&gOriginalPicFrame,-gOriginalPicFrame.left,-gOriginalPicFrame.top);
  403.         if ( gOversample ) {
  404.             gOriginalPicFrame.right *= 2;
  405.             gOriginalPicFrame.bottom *= 2;
  406.         }
  407.         gRect = gOriginalPicFrame;
  408.         zpFrame = gRect;
  409.     
  410.         if ( (result=AllocateBuffers()) != 0 ) 
  411.             goto done;
  412.         OffsetRect(&zpFrame,50,50);
  413.         gSrcWindow = MakeSWindow(&zpFrame,theSFR.sfFile.name,gWorld);
  414.         SetGWorld((CGrafPtr)gWorld,nil);
  415.         if ( (result=DrawPictureFile(oFile,&gWorld->portRect,nil)) != 0 ) {
  416.             Error("DrawPictureFile Failed",result);
  417.             goto done;
  418.         }
  419.     } else {
  420.  
  421.         zpFrame = gRect;
  422.         OffsetRect(&zpFrame,70,70);
  423.         gAltWindow = MakeSWindow(&zpFrame,theSFR.sfFile.name,gAltWorld);
  424.         SetGWorld((CGrafPtr)gAltWorld,nil);
  425.         if ( (result=DrawPictureFile(oFile,&gAltWorld->portRect,nil)) != 0 ) {
  426.             Error("DrawPictureFile Failed",result);
  427.             goto done;
  428.         }
  429.     }
  430. done:
  431.     if ( oFile != -1 )
  432.         FSClose(oFile);
  433.     SetGWorld(saveWorld,saveGD);
  434.     return(result);
  435. }    /* End of () */